package com.recSys.util;

import com.recSys.model.Organization;
import com.recSys.model.User;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class OAuthHandler {

	public static Map<String,Object> ApiValidateUserLogin(JSONObject obj){
		Map<String,Object> map = new HashMap<String,Object>();
		if(obj.has("token")){
			JSONObject tokenObj = obj.getJSONObject("token");
			if(tokenObj.has("token")&&tokenObj.has("userType")&&tokenObj.getInt("userType")==2){
	            String url = "/loginRecord/authentication";
	    		JSONObject result = JSONObject.fromObject(HttpClientHandler.doPost(ApiServerEnum.OAUTH, tokenObj, url));
	    		if(result.getInt("errorcode")==2000){
	    			JSONObject info = result.getJSONObject("result").getJSONObject("info");
    				User temp = new User();
	    			temp.setId(info.getInt("id"));
	    			temp.setTrueName(info.getString("trueName"));
	    			temp.setPortrait(info.getString("portrait"));
	    			map.put("user", temp);
	    			return map;
	    		}
	    		map.put("errorcode", 3008);
				return map;
			}
			map.put("errorcode", 3008);
			return map;
        }
		map.put("errorcode", 3007);
		return map;
	}
	
	public static Map<String,Object> ApiValidateOrgLogin(JSONObject obj){
		Map<String,Object> map = new HashMap<String,Object>();
		if(obj.has("token")){
			JSONObject tokenObj = obj.getJSONObject("token");
			if(tokenObj.has("token")&&tokenObj.has("userType")&&tokenObj.getInt("userType")==3){
	            String url = "/loginRecord/authentication";
	    		JSONObject result = JSONObject.fromObject(HttpClientHandler.doPost(ApiServerEnum.OAUTH, tokenObj, url));
	    		if(result.getInt("errorcode")==2000){
	    			JSONObject info = result.getJSONObject("result").getJSONObject("info");
    				Organization temp = new Organization();
    				temp.setId(info.getInt("id"));
    				temp.setOrgName(info.getString("orgName"));
	    			map.put("organization", temp);
	    			return map;
	    		}
	    		map.put("errorcode", 3008);
				return map;
			}
			map.put("errorcode", 3008);
			return map;
        }
		map.put("errorcode", 3007);
		return map;
	}

	public static void validateLogin(HttpServletRequest request){
		Map<String,Object> map = getOAuthInfo(request);
		if(map.get("token")!=null && map.get("userType")!=null){
			String url = "/loginRecord/authentication";
			JSONObject obj = JSONObject.fromObject(map);
			JSONObject result = JSONObject.fromObject(HttpClientHandler.doPost(ApiServerEnum.OAUTH, obj, url));
			if(result.getInt("errorcode")==2000){
				JSONObject info = result.getJSONObject("result").getJSONObject("info");
				if((Integer)map.get("userType")==2){
	    			/*User temp = (User)JSONObject.toBean(info,User.class);*/
					User temp = new User();
					temp.setId(info.getInt("id"));
					temp.setTrueName(info.getString("trueName"));
					temp.setPortrait(info.getString("portrait"));
					request.getSession().setAttribute("user", temp);
					request.getSession().removeAttribute("organization");
				}else if ((Integer)map.get("userType")==3){
    				/*Organization temp = (Organization)JSONObject.toBean(info,Organization.class);*/
					Organization temp = new Organization();
					temp.setId(info.getInt("id"));
					temp.setUserName(info.getString("userName"));
					temp.setOrgEmail(info.getString("orgEmail"));
					temp.setPortrait(info.getString("portrait"));
					temp.setOrgName(info.getString("orgName"));
					temp.setState(info.getInt("state"));
					request.getSession().setAttribute("organization", temp);
					request.getSession().removeAttribute("user");
				}
			}else if(result.getInt("errorcode")==2008){
				request.getSession().removeAttribute("user");
				request.getSession().removeAttribute("organization");
			}
		}else{
			request.getSession().removeAttribute("user");
			request.getSession().removeAttribute("organization");
		}
	}

	public static void validateLogout(HttpServletRequest request){
		Map<String,Object> map = getOAuthInfo(request);
		if(map.get("token")!=null && map.get("userType")!=null){
			String url = "/loginRecord/logout";
			JSONObject obj = JSONObject.fromObject(map);
			HttpClientHandler.doPost(ApiServerEnum.OAUTH, obj, url);
		}
	}

	public static Map<String,Object> getOAuthInfo(HttpServletRequest request){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("userType", null);
		map.put("token", null);
		map.put("userIP", CusAccessObjectUtil.getIpAddress(request));
		if(request.getSession().getAttribute("token")!=null&&request.getSession().getAttribute("userType")!=null){
			map.put("userType", (int)request.getSession().getAttribute("userType"));
			map.put("token", request.getSession().getAttribute("token"));
		}else{
			/*Cookie[] cookies = request.getCookies();
			if(cookies!=null){
				String tokenStr = null;
		        for(Cookie c :cookies ){
		            if(c.getName().equals("token")){
		            	tokenStr = c.getValue();
		            }
		        }
		        if(tokenStr!=null){
		        	String[] cookiesValue = tokenStr.split(",");
		        	if(cookiesValue.length==2){
			            map.put("token", cookiesValue[0]);
			        	map.put("userType", Integer.parseInt(cookiesValue[1]));
		        	}
		        }
			}*/
		}
		return map;
	}
}
